package com.yuanfengPlus.retailManage.retailback.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuanfengPlus.TaskManage.dao.TodoTaskDao;
import com.yuanfengPlus.TaskManage.model.TodoTask;
import com.yuanfengPlus.retailManage.retailback.DTO.RetailBackDTO;
import com.yuanfengPlus.retailManage.retailback.DTO.RetailBackSelectDTO;
import com.yuanfengPlus.retailManage.retailback.dao.RetailBackDao;
import com.yuanfengPlus.retailManage.retailback.model.RetailReturnOrder;
import com.yuanfengPlus.retailManage.retailback.service.IRetailBackService;
import com.yuanfengPlus.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class IRetailBackServiceImpl implements IRetailBackService {
@Autowired
    private RetailBackDao retailBackDao;
@Autowired
    private TodoTaskDao todoTaskDao;
    @Override
    public Result insertRetailBack(RetailBackDTO retailBackDTO) {
        RetailReturnOrder retailReturnOrder = retailBackDTO.getRetailReturnOrder();
        if (retailReturnOrder.getRetailOutboundOrderNumber()== null){
            return Result.failure(400,"零售退货单必须关联零售出库单");
        }
        int i = retailBackDao.insertRetailBack(retailReturnOrder);
        if (i <= 0){
            return Result.failure(500,"添加失败");
        }else{
            TodoTask todoTask = new TodoTask();
            todoTask.setTaskId(retailReturnOrder.getRetailReturnOrderNumber()+"todoTask");
            todoTask.setTaskDescription("零售退货");
            //读取零售退货人
            todoTask.setInitiator(retailBackDTO.getUserName());
            //添加处理人名字
            todoTask.setProcessor("零售经理");
            //读取本地时间待办发起时间
            todoTask.setProcessingTime(new Date());
            int insert = todoTaskDao.insert(todoTask);
            if (insert <= 0){
                return Result.failure(500,"添加失败");
            }else{
                return Result.success("添加成功");
            }
        }
    }
    // 使用pagehelper做分页查询
    @Override
    public Result queryRetailBack(RetailBackSelectDTO retailBackSelectDTO) {
        // 从DTO中获取动态的页码和每页显示条数
        int pageNum = retailBackSelectDTO.getPageNum(); // 页码
        int pageSize = retailBackSelectDTO.getPageSize(); // 每页显示条数
        // 使用PageHelper开始分页
        PageHelper.startPage(pageNum, pageSize);
        // 执行查询
        List<RetailReturnOrder> retailReturnOrders = retailBackDao.selectRetailBack(retailBackSelectDTO.getRetailReturnOrder());
        // 获取分页信息
        PageInfo<RetailReturnOrder> pageInfo = new PageInfo<>(retailReturnOrders);
        // 返回包含分页信息的结果
        return Result.success(pageInfo);
    }
    //审核退货单

}
